package com.example.smart.handler;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Setter;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Setter
@Component
public class AuthenticationSuccessHanders implements AuthenticationSuccessHandler {
    // 登录成功后的访问地址
    private String url;
    // 是否重定向
    private boolean isRedirect;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        if(isRedirect){
            // 重定向
            response.sendRedirect(url);
        }else{
            // 请求转发
            request.getRequestDispatcher(url).forward(request, response);
        }
    }
}
